<?php

namespace App\Kuafu\Shop\Enum;

use Illuminate\Cache\Repository;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;

class DeviceCacheEnum
{

    const DEVICE_SHOP_TV_ADV = "device-adv:serial-no:%s";

    /**
     * 设备广告缓存key
     *
     * @param string $serialNo
     * @return string
     */
    public static function cacheKeyDeviceShopTvAdv(string $serialNo): string
    {
        return vsprintf(self::DEVICE_SHOP_TV_ADV, [$serialNo]);
    }

    /**
     * 移除电视广告缓存
     */
    public static function rmDeviceShopTvAdvCache(string $serialNo)
    {
        Cache::store("redis")->delete(self::cacheKeyDeviceShopTvAdv($serialNo));
    }

    /**
     * 批量移除电视广告缓存
     */
    public static function batchRmDeviceShopTvAdvCache()
    {
        $redisClient = Redis::connection("cache")->client();
        $cachePrefix = config("cache.prefix");
        $keys        = $redisClient->keys("{$cachePrefix}:device-adv:serial-no:*");
        $redisPrefix = $redisClient->getOption(\Redis::OPT_PREFIX);
        $keys        = collect($keys)->map(function ($v) use ($redisPrefix) {
            return str_replace($redisPrefix, "", $v);
        })->values()->toArray();

        if (!empty($keys)) {
            $redisClient->del($keys);
        }
    }

}
